package com.hyf.serviceedu.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyf.service.config.exception.GuliException;
import com.hyf.serviceedu.entity.EduCourse;
import com.hyf.serviceedu.entity.EduCourseDescription;
import com.hyf.serviceedu.entity.condition.CoursePublishVo;
import com.hyf.serviceedu.entity.condition.CourseQuery;
import com.hyf.serviceedu.entity.condition.CourseinfoVo;
import com.hyf.serviceedu.entity.vo.CourseQueryVo;
import com.hyf.serviceedu.entity.vo.CourseWebVo;
import com.hyf.serviceedu.mapper.EduCourseMapper;
import com.hyf.serviceedu.service.EduChapterService;
import com.hyf.serviceedu.service.EduCourseDescriptionService;
import com.hyf.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyf.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-04-21
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired// 课程描述的注入
    private EduCourseDescriptionService descriptionService;// 注入课程简介表的 service

    // 注入小结和章节的 service
    @Autowired
    private EduVideoService eduVideoService;

    // 注入课程本身的service
    @Autowired
    private EduChapterService eduChapterService;



    @Override// 添加课程基本信息的方法
    public String saveCourseinfo(CourseinfoVo courseinfovo) {
        // 向课程表中添加课程基本信息
        // CourseinfoVo 对象转换 为 EduCourse 对象
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseinfovo, eduCourse);// 向课程表中添加课程的基本信息
       int insert= baseMapper.insert(eduCourse);
       if(insert<=0){
           // 添加失败
           throw new GuliException(20001,"添加课程失败");
       }

       // 获取添加之后的课程id
        String cid =eduCourse.getId();// 获取添加之后的id

        // 向课程简介表添加课程简介
        // edu_course_description  课程简介表
        // 注入 EduCourseDescription 这个表的信息
        // 注入 实体类 课程简介表的实体类
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseinfovo.getDescription());
        courseDescription.setId(cid);// 设置描述 id 就是 课程 id
        boolean save = descriptionService.save(courseDescription);// 实现了向描述表中添加数据
        if(!save){
            throw new GuliException(20001, "课程详情信息保存失败");
        }
        return cid;// 当表中的数据无法加入的时候 报 subject_parent_id 该字段不能为 空的时候设置数据库，把他改为可以为空
    }



    // 根据 课程id 查询课程基本信息
    @Override
    public CourseinfoVo getCourseInfo(String courseId) {

        // 1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        //封装
        CourseinfoVo courseInfo=new CourseinfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfo);

        // 2.查询描述表
        EduCourseDescription byIdCourseDescription = descriptionService.getById(courseId);
        courseInfo.setDescription(byIdCourseDescription.getDescription());// 封装


        return courseInfo;
    }

    // 修改课程信息
    @Override
    public void updateCourseinfo(CourseinfoVo courseinfovo) {
        // 先修改课程表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseinfovo,eduCourse);
        int updateById = baseMapper.updateById(eduCourse);

        if(updateById==0){
            throw new GuliException(20001,"修改课程失败");
        }

        // 2 修改描述表
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseinfovo.getId());
        description.setDescription(courseinfovo.getDescription());
        descriptionService.updateById(description);

    }

    /// 根据课程 id 查询 课程确认信息
    @Override
    public CoursePublishVo publishCourseinfo(String id) {
        // 调用 mapper
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishVo(id);

        return coursePublishVo;
    }


    // 删除课程的方法
    @Override
    public void removeCourseById(String courseId) {
     // 根据课程id 删除小结
        eduVideoService.removeVideoByIdCourseId(courseId);
        // 根据课程id 删除 章节
        eduChapterService.removeChapterByCourseId(courseId);
        // 根据课程id 删除描述
        descriptionService.removeById(courseId);
        // 根据课程id 删除课程本身
        int result= baseMapper.deleteById(courseId);
        if(result == 0){
            throw new GuliException(20001,"删除失败");
        }
    }











    // 分页条件查询
    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }
    
    
    // 前端查询课程列表
    @Override
    public Map<String, Object> getCoursefronlist(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        // 根据讲师id查询所有课程
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        // 判断条件值是否为空，不为空拼接
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {// 一级分类
            
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
            
        }
    
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {// 二机分类
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
            
        }
       
        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {// 销量排序 关注度
            // 倒叙
            queryWrapper.orderByDesc("buy_count");
            
        }
        
        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {//时间排序
            
            queryWrapper.orderByDesc("gmt_create");
            
        }
        
        
        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {// 价格排序
            queryWrapper.orderByDesc("price");// 降序排列
            
        }
      
        baseMapper.selectPage(pageParam, queryWrapper);// 调用查询方法
       
        List<EduCourse> records = pageParam.getRecords();
        
        long current = pageParam.getCurrent();
        
        long pages = pageParam.getPages();
        
        long size = pageParam.getSize();
        
        long total = pageParam.getTotal();
        
        boolean hasNext = pageParam.hasNext();
        
        boolean hasPrevious = pageParam.hasPrevious();
       
        Map<String, Object> map = new HashMap<String, Object>();
        
        map.put("items", records);
      
        map.put("current", current);
        
        map.put("pages", pages);
      
        map.put("size", size);
    
        map.put("total", total);
   
        map.put("hasNext", hasNext);
        
        map.put("hasPrevious", hasPrevious);
  
        return map;
     
    }
    
    // 根据课程id查询课程
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
    
    
    /**
     * 根据讲师id查询当前讲师的课程列表
     * @param teacherId
     * @return
     */

    @Override
    public List<EduCourse> selectByTeacherId(String teacherId) {
      
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<EduCourse>();
        
        queryWrapper.eq("teacher_id", teacherId);
        
        //按照最后更新时间倒序排列
        
        queryWrapper.orderByDesc("gmt_modified");
       
        List<EduCourse> courses = baseMapper.selectList(queryWrapper);
        
        return courses;
        
    }

}
